package com.mc.developmentkit.https;

import android.os.Handler;
import android.os.Message;
import android.util.Base64;
import android.util.Log;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;
import java.io.UnsupportedEncodingException;
import java.util.Map;

/**
 * Created by LeBron on 2016/12/22.
 */

public class HttpUtils {

    /**
     * Get网络请求
     * @param url
     * @return
     */
    public static void GET(final Handler handler, String url) {

        RequestParams params = new RequestParams(url);
        params.setConnectTimeout(10 * 1000);
        params.setCacheMaxAge(1000 * 60);
        final Message msg = new Message();
        x.http().get(params, new Callback.CacheCallback<String>() {
            @Override
            public void onCancelled(CancelledException arg0) {
            }
            @Override
            public void onError(Throwable arg0, boolean arg1) {
                Log.e("GET错误信息", arg0.toString());
                msg.what = 2;
                msg.obj = arg0;
                handler.sendMessage(msg);
            }
            @Override
            public void onFinished() {
            }

            @Override
            public void onSuccess(String json) {
                msg.what = 1;
                msg.obj = json;
                handler.sendMessage(msg);
            }

            @Override
            public boolean onCache(String json) {
                msg.what = 1;
                msg.obj = json;
                handler.sendMessage(msg);
                return true;
            }
        });
    }

    /**
     * Post网络请求
     * @param url
     * @return
     */
    public static void POST(final Handler handler, String url, String body, final boolean encryption) {

        RequestParams params = new RequestParams(url);
        if (encryption) {
            String encodeToString = Base64.encodeToString(body.toString().getBytes(), Base64.DEFAULT);
            params.setBodyContent(encodeToString);
        } else {
            params.setBodyContent(body);
        }
        params.setCacheMaxAge(1000 * 60);
        final Message msg = new Message();
        x.http().post(params, new Callback.CacheCallback<String>() {
            @Override
            public void onCancelled(CancelledException arg0) {
            }

            @Override
            public void onError(Throwable arg0, boolean arg1) {
                Log.e("POST错误信息", arg0.toString());
                msg.what = 2;
                handler.sendMessage(msg);
            }

            @Override
            public void onFinished() {
            }

            @Override
            public void onSuccess(String json) {
                msg.what = 1;
                try {
                    if (encryption) {
                        String result = new String(Base64.decode(json, Base64.DEFAULT), "utf-8");
                        msg.obj = result;
                        handler.sendMessage(msg);
                    } else {
                        msg.obj = json;
                        handler.sendMessage(msg);
                    }
                } catch (Exception e) {
                    Log.e("POST+json成功回调出错：", e.toString());
                }
            }

            @Override
            public boolean onCache(String json) {
                msg.what = 1;
                try {
                    String result = new String(Base64.decode(json, Base64.DEFAULT), "utf-8");
                    msg.obj = result;
                    handler.sendMessage(msg);
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                    Log.e("POST+json缓存回调出错：", e.toString());
                }
                return true;
            }
        });
    }



    /**
     * Post网络请求
     * @param url
     * @return
     */
    public static void POST(final Handler handler, String url, Map<String, String> map, final boolean aa) {

        RequestParams params = new RequestParams(url);
        if(map!=null){
            for (Map.Entry<String, String> entry : map.entrySet()) {
                params.addBodyParameter(entry.getKey(),entry.getValue());
            }
        }
        final Message msg = new Message();
        x.http().post(params, new Callback.CacheCallback<String>() {
            @Override
            public void onCancelled(CancelledException arg0) {
            }

            @Override
            public void onError(Throwable arg0, boolean arg1) {
                Log.e("POST错误信息", arg0.toString());
                msg.what = 2;
                handler.sendMessage(msg);
            }

            @Override
            public void onFinished() {
            }

            @Override
            public void onSuccess(String json) {
                msg.what = 1;
                try {
                    if (aa) {
                        Log.e("搜索返回数据",json);
                        String result = new String(Base64.decode(json, Base64.DEFAULT), "utf-8");
                        msg.obj = result;
                        handler.sendMessage(msg);
                    } else {
                        if (json != null && json.startsWith("\ufeff")) {
                            json = json.substring(1);
                        }
                        msg.obj = json;
                        handler.sendMessage(msg);
                    }
                } catch (Exception e) {
                    Log.e("POST+json成功回调出错：", e.toString());
                }
            }

            @Override
            public boolean onCache(String json) {
                return false;
            }
        });
    }
}
